package com.xyx.controller.user;

import com.github.pagehelper.PageInfo;
import com.xyx.entity.Result;
import com.xyx.pojo.PasswordForm;
import com.xyx.pojo.SysUser;
import com.xyx.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private ISysUserService sysUserService;
    /*
    去主页面
     */
    @RequestMapping("/main")
    public  String main(HttpSession httpSession,Model model){
        SysUser user =(SysUser) httpSession.getAttribute("USER_SESSION");
        model.addAttribute("name",user.getName());
        return "main";
    }
    /*
   去登录
    */
    @RequestMapping("/to_login")
    public String to_login(){
        return "login";
    }
    /*
    去首页
     */
    @RequestMapping("/index")
    public  String index(){

        return "index";
    }
    /*
        去登录验证
 */
    @RequestMapping("/login")
    public  String login(SysUser sysUser , Model model, HttpSession httpSession){
        System.out.println("收到数据");
        System.out.println(sysUser);
        //安全验证
        if(sysUser==null || sysUser.getUsername()==null
                || sysUser.getUsername().trim().equals("")
                ||sysUser.getPassword()==null
                ||sysUser.getPassword().trim().equals("")
        ){
            model.addAttribute("msg","请输入用户名或密码");
            return "login";
        }

        SysUser user = sysUserService.getUserByUsernameAndPwd(sysUser);
        System.out.println(user);
        if(user!=null){
            httpSession.setAttribute("USER_SESSION",user);
            model.addAttribute("name",user.getName());
            model.addAttribute("password",user.getPassword());
            model.addAttribute("id",user.getId());
            return "main";
        }else{
            model.addAttribute("msg","用户名或密码错误！");
            return "login";
        }
    }

    /**
     * 用户列表查询
     * @param username
     * @param model
     * @return
     */
    @RequestMapping("/user_list")
    public  String user_list(String username,Model model ,
        @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
        @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){

        List<SysUser> allUsers = sysUserService.getAllUsers(username, pageNum, pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(allUsers);
       // System.out.println(pageInfo);
        model.addAttribute("user_list",allUsers);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("username",username);
        return "user_list";
    }
    /**
     * 删除用户
     */
    @RequestMapping("delUser")
    @ResponseBody
    public Result deleteUserById(Integer id){
        Result<Object> result=new Result<>();
        if(id!=null){
            int i=sysUserService.deleteUserById(id);
            if(i>0){
                System.out.println("删除成功");
                result.setMsg("删除成功");
                result.setSuccess(true);
                return   result;
            } else {
                System.out.println("删除失败");
                result.setMsg("删除失败");
                result.setSuccess(false);
                return   result;
            }
        }else {
            System.out.println("请选择要删除的ID！");
            result.setMsg("请选择要删除的ID");
            result.setSuccess(false);
            return   result;
        }
    }
    /**
     * 批量删除用户
     */
    @RequestMapping("delAll")
    @ResponseBody
    public Result deleteAll(String ids) {
        Result<Object> result = new Result<>();
        System.out.println("收到删除数据");
        System.out.println(ids);
        if(ids!=null && !ids.trim().equals("")){

            String[] strArray = ids.split(",");
            List<String> idList = Arrays.asList(strArray);

            int i = sysUserService.deleteAll(idList);
            if(i>0){
                System.out.println("删除成功");
                result.setMsg("删除成功");
                result.setSuccess(true);
                return   result;
            } else {
                System.out.println("删除失败");
                result.setMsg("删除失败");
                result.setSuccess(false);
                return   result;
            }
        }else {
            System.out.println("请选择数据！");
            result.setMsg("请选择数据");
            result.setSuccess(false);
            return   result;
        }
    }


    /**
     * 新增用户
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public Result addUser(SysUser sysUser){
        System.out.println(sysUser);
        Result<Object> result=new Result<>();
        if(sysUser!=null
                && sysUser.getUsername()!=null
                && !sysUser.getUsername().trim().equals("")
                && sysUser.getPassword()!=null
                && !sysUser.getPassword().trim().equals("")
        ){
            System.out.println(sysUser);
            SysUser user = sysUserService.getUserByUsername(sysUser.getUsername());
            if(user!=null){
                result.setMsg("用户已经存在");
                result.setSuccess(false);
                return   result;

            }
            int i = sysUserService.addUser(sysUser);
            if(i>0){
                System.out.println("新增成功");
                result.setMsg("新增成功");
                result.setSuccess(true);
                return   result;
            }else {
                System.out.println("新增失败");
                result.setMsg("新增失败");
                result.setSuccess(false);
                return   result;
            }
        }else {
            System.out.println("请输入用户名和密码！");
            result.setMsg("请输入用户名和密码");
            result.setSuccess(false);
            return   result;
        }

    }
    @RequestMapping("/getUserById")
    @ResponseBody
    public Result getUserById(Integer id){
        Result<Object> result=new Result<>();
        if(id!=null){
            SysUser user = sysUserService.getUserById(id);

            if(user!=null){
                result.setData(user);
                System.out.println("查找成功");
                result.setMsg("查找成功");
                result.setSuccess(true);
                return   result;
            } else {
                System.out.println("没有此用户");
                result.setMsg("没有此用户");
                result.setSuccess(false);
                return   result;
            }
        }else {
            System.out.println("请选择ID！");
            result.setMsg("请选择ID");
            result.setSuccess(false);
            return   result;
        }

    }
    /**
     * 编辑用户
     */
    @RequestMapping("/editUser")
    @ResponseBody
    public Result editUser(SysUser sysUser){
        System.out.println(sysUser);
        Result<Object> result=new Result<>();
        if(sysUser!=null
                && sysUser.getUsername()!=null
                && !sysUser.getUsername().trim().equals("")
                && sysUser.getId()!=null
        ){
            System.out.println(sysUser);
            int i = sysUserService.editUser(sysUser);
            if(i>0){
                System.out.println("修改成功");
                result.setMsg("修改成功");
                result.setSuccess(true);
                return   result;
            }else {
                System.out.println("修改失败");
                result.setMsg("修改失败");
                result.setSuccess(false);
                return   result;
            }
        }else {
            System.out.println("请输入用户名和ID！");
            result.setMsg("请输入用户名和ID");
            result.setSuccess(false);
            return   result;
        }

    }
    @RequestMapping("/editPassword")
    @ResponseBody
    public Result editPassword(Integer id, String password){
        Result<Object> result = new Result<>();
        if (

                id!=null
                        && password!=null
        ){
            int i = sysUserService.editPassword(id,password);
            if (i>0){
                System.out.println("修改成功！");
                result.setMsg("修改成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("修改失败！");
                result.setMsg("修改失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入密码！");
            result.setMsg("请输入密码！");
            result.setSuccess(false);
            return result;
        }

    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "login";
    }
}